package com.htfyun.roobofactory.factorytest;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.TestKeysAttr;
import com.roobo.hardware.KeyEventManager;

/**
 * Created by sst on 2018/7/8.
 */

public class KeysTestActivity extends BaseActivity {

    private Button btnPass;
    private Button btnFail;

    private GridView gridView;
    private KeyAdapter keyAdapter;

    private int flagKeyDownTest = 0;
    private int flagKeyUpTest = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_factorytest_keys);

        gridView = (GridView) findViewById(R.id.gridView);

        keyAdapter = new KeyAdapter(this, -1, TestKeysAttr.values());

        gridView.setAdapter(keyAdapter);

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);


        for (TestKeysAttr attr : TestKeysAttr.values()) {
            flagKeyDownTest |= attr.getFlagKeyDown();
            flagKeyUpTest |= attr.getFlagKeyUp();
        }
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        TestKeysAttr attr = getKeyAttrByKeyCode(keyCode);
        if (attr != null) {
            flagKeyDownTest &= ~attr.getFlagKeyDown();

            keyAdapter.notifyDataSetChanged();

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        TestKeysAttr attr = getKeyAttrByKeyCode(keyCode);
        if (attr != null) {
            flagKeyDownTest |= attr.getFlagKeyDown();
            flagKeyUpTest &= ~attr.getFlagKeyUp();

            keyAdapter.notifyDataSetChanged();

            if (flagKeyUpTest == 0) {
                startAutoPass(R.string.test_keys_pass);
            }

            return true;
        }
        return super.onKeyUp(keyCode, event);
    }


    private TestKeysAttr getKeyAttrByKeyCode(final int keyCode) {
        for (TestKeysAttr attr : TestKeysAttr.values()) {
            if (attr.getKeyCode() == keyCode) {
                return attr;
            }
        }
        return null;
    }


    private void setBtnPassOrFailEnabled(boolean enabled) {
        btnFail.setEnabled(enabled);
        btnPass.setEnabled(enabled);
    }

    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };

    private final class KeyAdapter extends ArrayAdapter<TestKeysAttr> {


        public KeyAdapter(@NonNull Context context, int resource, @NonNull TestKeysAttr[] objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_keys, null);
                holder.txt = (TextView) convertView.findViewById(R.id.txtKeys);

                convertView.setTag(holder);
            }

            TestKeysAttr attr = getItem(position);
            if (attr != null) {
                if ((flagKeyDownTest & attr.getFlagKeyDown()) == 0) {

                    holder.txt.setBackgroundColor(Color.YELLOW);//getColor(R.color.colormYellow));

                } else  if ((flagKeyUpTest & attr.getFlagKeyDown()) == 0) {

                    holder.txt.setBackgroundColor(getColor(R.color.colorGreen));

                } else {

                    holder.txt.setBackgroundColor(getColor(R.color.transparent));

                }

                String keyInfo = getString(attr.getNameResId());

                if ((flagKeyUpTest & attr.getFlagKeyDown()) == 0) {

                    keyInfo += getString(R.string.arrow) + getString(R.string.test_result_pass);

                }

                holder.txt.setText(keyInfo);
            }
            return convertView;
        }
    }

    private final class ViewHolder {
        TextView txt;
    }

}
